/*
 * @Author: yanyqf
 * @Date: 2020-03-06 10:51:56
 * @LastEditTime: 2020-04-21 11:43:40
 * @Description: file content
 */


import React, { Component, Fragment } from 'react'
import creactUIParams from './UIIndex.js'
import config from 'utils/config.js'
import App from 'layout/App.js';
console.log(config, 'config')
import axios from 'axios';
import Loading from '../Loading/index.jsx'



function autoBindHandleToC() {
    let fns = this.state.fns;

    this.searchC && (fns.search = this.searchC.search);

    this.treeCard && (fns.treeCard = this.treeCard.output);

    this.button && (fns.button = this.button.output);

    this.form && (fns.form = this.form.output);

    this.promptModal && (fns.promptModal = this.promptModal.output);

    this.approve && (fns.approve = this.approve.output);

    this.passreject && (fns.passreject = this.passreject.output);

    this.approveHistory && (fns.approveHistory = this.approveHistory.output);

    return fns
}

let createYGCPage = (pageParams = {}) => (_Component) => {
    return class CommonComponent extends Component {
        constructor(props) {
            super(props);
            this.state = {
                fns: {
                    ajax: ajax.bind(this)
                }
            }
            creactUIParams.call(this)
        }

        render() {
            return (
                <App>
                    <_Component
                        {...pageParams}
                        {...this.state.fns}
                        {...config}
                        {...this.props}
                        ref={el => this._Component = el}
                        YGCCreateSearch={this.YGCCreateSearch}
                        YGCreateCardTitle={this.YGCreateCardTitle}
                        YGCCreateTreeCard={this.YGCCreateTreeCard}
                        YGCreateButton={this.YGCreateButton}
                        YGCCreateForm={this.YGCCreateForm}
                        YGCreateApproveModal={this.YGCreateApproveModal}
                        YGCreatePassRejectModal={this.YGCreatePassRejectModal}
                        YGCreateAproHistoryModal={this.YGCreateAproHistoryModal}
                    />
                    {this.YGCCreatePromptModal()}
                    <Loading ref={el => this.Loading = el} />
                </App>

            )
        }

        componentDidMount() {
            this.setComponentFn()
        }

        setComponentFn = () => {
            let fns = autoBindHandleToC.call(this)
            this.setState({
                fns
            }, () => {
                this._Component.initTplAfter && this._Component.initTplAfter()
            })
        }
    }
};



function ajax ({ url, method = 'post', data, success, error }){
    !this.Loading.getLoadingState() && this.Loading.setLoadingState(true)
    let axiosParams = {
        method,
        url,
        headers: {
            'Cache-Control': 'no-cache',
        }
    }
    if (method == 'post') {
        axiosParams = { ...axiosParams, data }
    } else if (method == 'get') {
        axiosParams = { ...axiosParams, params: data }
    }
    axios(axiosParams).then((res) => {
        this.Loading.setLoadingState(false)
        let data = res.data;
        if (data.resultCode === 'ok') {
            success && success(res)
        } else {
            if (error) {
                error(data.exMsg)
            } else {
                config.vendorsDanger({ 'text': data.exMsg })
            }
        }
    })
}

export default createYGCPage

